package com.yq.testmodule.vp2;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.viewpager2.widget.ViewPager2;

public class ScaleInTransformer implements ViewPager2.PageTransformer {
    private static final float DEFAULT_MIN_SCALE = 0.85f;
    private float mTopScale = DEFAULT_MIN_SCALE;
    private float mBottomScale = DEFAULT_MIN_SCALE;

    public ScaleInTransformer() {
    }

    public ScaleInTransformer(float topScale,float bottomScale) {
        this.mTopScale = topScale;
        this.mBottomScale = bottomScale;
    }

    @Override
    public void transformPage(@NonNull View view, float position) {
        int pageWidth = view.getWidth();
        int pageHeight = view.getHeight();

        view.setPivotY(pageHeight / 2);
        view.setPivotX(pageWidth / 2);
        if (position < -1) { // [-Infinity,-1)
            view.setScaleX(mTopScale);
            view.setScaleY(mTopScale);
            view.setPivotY(0);
        } else if (position <= 1) { // [-1,1]
            // Modify the default slide transition to shrink the page as well
            if (position < 0) //1-2:1[0,-1] ;2-1:1[-1,0]
            {

                float scaleFactor = (1 + position) * (1 - mTopScale) + mTopScale;
                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);

                view.setPivotY(0);

            } else //1-2:2[1,0] ;2-1:2[0,1]
            {
                float scaleFactor = (1 - position) * (1 - mBottomScale) + mBottomScale;
                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);
                view.setPivotY(pageHeight / 6);
            }


        } else { // (1,+Infinity]
            view.setPivotY(pageHeight / 6);
            view.setScaleX(mBottomScale);
            view.setScaleY(mBottomScale);
        }
    }
}
